package com.newray.base.spring.web.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

public class DoubleSubmitInterceptor implements HandlerInterceptor {
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object paramObject) throws Exception {
		String url = request.getRequestURL().toString();
		HttpSession session = request.getSession();
		Object s_token = session.getAttribute(url);
		if (s_token == null) {
			session.setAttribute(url, request.getParameter("token"));
			return true;
		}
		if (s_token.equals(request.getParameter("token"))) {
			return true;
		}

		session.setAttribute(url, request.getParameter("token"));

		return true;
	}

	public void postHandle(HttpServletRequest paramHttpServletRequest,
			HttpServletResponse paramHttpServletResponse, Object paramObject,
			ModelAndView paramModelAndView) throws Exception {
		System.out.println(paramObject);
	}

	public void afterCompletion(HttpServletRequest paramHttpServletRequest,
			HttpServletResponse paramHttpServletResponse, Object paramObject,
			Exception paramException) throws Exception {
		System.out.println(paramObject);
	}
}